using AutoMapper;
using MediatR;
using Medical.Api.Write.Application.Command.BasicManagement.DeviceDictionaryCommand;
using Medical.Domain.BasicManagement;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;

namespace Medical.Api.Write.Application.Handler.BasicManagement.DeviceDictionaryHandler
{
    /// <summary>
    /// 更新设备信息处理器
    /// </summary>
    public class UpdateEquipmentHandler : IRequestHandler<UpdateEquipmentCommand, ApiResult<bool>>
    {
        private readonly IBaseRepository<Equipment> _equipmentRepository;
        private readonly ILogger<UpdateEquipmentHandler> _logger;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        public UpdateEquipmentHandler(
            IBaseRepository<Equipment> equipmentRepository,
            ILogger<UpdateEquipmentHandler> logger,
            IMapper mapper)
        {
            _equipmentRepository = equipmentRepository;
            _logger = logger;
            _mapper = mapper;
        }

        /// <summary>
        /// 处理更新设备信息请求
        /// </summary>
        public Task<ApiResult<bool>> Handle(UpdateEquipmentCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 1. 查找设备
                var equipment = _equipmentRepository.GetModel(request.EquipmentData.Id);
                if (equipment == null)
                {
                    _logger.LogWarning($"无法更新设备信息，设备ID {request.EquipmentData.Id} 不存在");
                    return Task.FromResult(new ApiResult<bool>
                    {
                        Code = ApiEnums.NotFound,
                        Msg = $"设备不存在，ID: {request.EquipmentData.Id}",
                        Data = false
                    });
                }

                // 2. 使用AutoMapper更新设备信息（不包括状态和删除标识）
                _mapper.Map(request.EquipmentData, equipment);

                // 3. 保存更改
                var result = _equipmentRepository.Update(equipment);

                // 4. 返回结果
                if (result > 0)
                {
                    _logger.LogInformation($"设备信息更新成功，ID: {equipment.Id}");
                    return Task.FromResult(new ApiResult<bool>
                    {
                        Code = ApiEnums.Success,
                        Msg = "设备信息更新成功",
                        Data = true
                    });
                }

                return Task.FromResult(new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = "设备信息更新失败",
                    Data = false
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新设备信息时发生异常");
                return Task.FromResult(new ApiResult<bool>
                {
                    Code = ApiEnums.Error,
                    Msg = $"处理请求时发生错误: {ex.Message}",
                    Data = false
                });
            }
        }
    }
}